home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / viewkit / VCal / Preferences.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  14.7 KB  |  492 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <malloc.h>
  20. #include "Preferences.h"
  21. #include "VCal.h"
  22. #include "Utils.h"
  23. #include "path.h"
  24. #include <Vk/VkResource.h>
  25. #include <Vk/VkWarningDialog.h>
  26.  
  27. Preferences *thePreferences = NULL;
  28.  
  29. Preferences::Preferences(Widget w)
  30. {
  31.   char *name, *adv, *cmd;
  32.  
  33.   if (name = VkGetResource(w, "defaultFilename", "DefaultFilename")) {
  34.     _filename = pathexpandtilde(name);
  35.   } else {
  36.     _filename = pathexpandtilde(DEFAULT_FILENAME);
  37.   }
  38.   _filename0 = strdup(_filename);
  39.   if (adv = VkGetResource(w, "advanceWarning", "AdvanceWarning")) {
  40.     _advanceWarning = strdup(adv);
  41.   } else {
  42.     _advanceWarning = strdup("5 min");
  43.   }
  44.   _advanceWarning0 = strdup(_advanceWarning);
  45.   _updateInterval = (int) VkGetResource(w, "updateIntervalInSeconds",
  46.                     "UpdateIntervalInSeconds", XmRInt,
  47.                     "60");
  48.   _updateInterval0 = _updateInterval;
  49.   _showLates = (Boolean) (int) VkGetResource(w, "showLates",
  50.                          "ShowLates", XmRBoolean,
  51.                          "False");
  52.   _showLates0 = _showLates;
  53.   _saveChangesAutomatically =
  54.     (Boolean) (int) VkGetResource(w, "saveChangesAutomatically",
  55.                   "SaveChangesAutomatically", XmRBoolean,
  56.                   "True");
  57.   _saveChangesAutomatically0 = _saveChangesAutomatically;
  58.   _annotateFile = (Boolean) (int) VkGetResource(w, "annotateFile",
  59.                         "AnnotateFile", XmRBoolean,
  60.                         "False");
  61.   _annotateFile0 = _annotateFile;
  62.   _updateIconName = (Boolean) (int) VkGetResource(w, "updateIconName",
  63.                           "UpdateIconName", XmRBoolean,
  64.                           "False");
  65.   _updateIconName0 = _updateIconName;
  66.   _updateIconDate = (Boolean) (int) VkGetResource(w, "updateIconDate",
  67.                           "UpdateIconDate", XmRBoolean,
  68.                           "False");
  69.   _updateIconDate0 = _updateIconDate;
  70.   _clock24 = (Boolean) (int) VkGetResource(w, "clock24",
  71.                        "Clock24", XmRBoolean,
  72.                        "False");
  73.   _clock240 = _clock24;
  74.   _alarms = (Boolean) (int) VkGetResource(w, "displayAlarms",
  75.                       "DisplayAlarms", XmRBoolean,
  76.                       "True");
  77.   _alarms0 = _alarms;
  78.   _notifyPopup = (Boolean) (int) VkGetResource(w, "notifyPopup",
  79.                            "NotifyPopup", XmRBoolean,
  80.                            "True");
  81.   _notifyPopup0 = _notifyPopup;
  82.   _notifyBell = (Boolean) (int) VkGetResource(w, "notifyBell",
  83.                           "NotifyBell", XmRBoolean,
  84.                           "True");
  85.   _notifyBell0 = _notifyBell;
  86.   _notifyMail = (Boolean) (int) VkGetResource(w, "notifyMail",
  87.                           "NotifyMail", XmRBoolean,
  88.                            "False");
  89.   _notifyMail0 = _notifyMail;
  90.   _notifyConfirmDelete = (Boolean) (int)
  91.     VkGetResource(w, "notifyConfirmDelete",
  92.           "NotifyConfirmDelete", XmRBoolean,
  93.           "True");
  94.   _notifyConfirmDelete0 = _notifyConfirmDelete;
  95.   if (cmd = VkGetResource(w, "notifyCommand", "NotifyCommand")) {
  96.     _notifyCommand = strdup(cmd);
  97.   } else {
  98.     _notifyCommand = strdup("");
  99.   }
  100.   _notifyCommand0 = strdup(_notifyCommand);
  101.   _annotateDay = (Boolean) (int) VkGetResource(w, "annotateDay",
  102.                            "AnnotateDay", XmRBoolean,
  103.                            "True");
  104.   _annotateDay0 = _annotateDay;
  105.   _snoozeMinutes = (int) VkGetResource(w, "snoozeMinutes",
  106.                        "SnoozeMinutes", XmRInt,
  107.                        "2");
  108.   _snoozeMinutes0 = _snoozeMinutes;
  109.   _hideMenuBar = (Boolean) (int) VkGetResource(w, "hideMenuBar",
  110.                            "HideMenuBar", XmRBoolean,
  111.                            "True");
  112.   _hideMenuBar0 = _hideMenuBar;
  113.   _apptLength = (int) VkGetResource(w, "apptLength",
  114.                     "ApptLength", XmRInt, "60");
  115.   _apptLength0 = _apptLength;
  116.   _gridLength = (int) VkGetResource(w, "gridLength",
  117.                     "GridLength", XmRInt, "60");
  118.   _gridLength0 = _gridLength;
  119.   _apptSubdiv = (int) VkGetResource(w, "apptSubdiv",
  120.                     "ApptSubdiv", XmRInt, "4");
  121.   _apptSubdiv0 = _apptSubdiv;
  122.   _snapInterval = (int) VkGetResource(w, "snapInterval",
  123.                       "SnapInterval", XmRInt, "30");
  124.   _snapInterval0 = _snapInterval;
  125.   _annotateMonth = (Boolean) (int) VkGetResource(w, "annotateMonth",
  126.                          "AnnotateMonth", XmRBoolean,
  127.                          "True");
  128.   _annotateMonth0 = _annotateMonth;
  129.   _restricted = (Boolean) (int) VkGetResource(w, "restricted",
  130.                           "Restricted", XmRBoolean,
  131.                           "True");
  132.   _restricted0 = _restricted;
  133.   _restrictStart = (int) VkGetResource(w, "restrictStart",
  134.                        "RestrictStart", XmRInt, "8");
  135.   _restrictStart0 = _restrictStart;
  136.   _restrictStop = (int) VkGetResource(w, "restrictStop",
  137.                       "RestrictStop", XmRInt, "19");
  138.   _restrictStop0 = _restrictStop;
  139.  
  140.   loadPreferences();
  141.  
  142.   thePreferences = this;
  143. }
  144.  
  145. Preferences::~Preferences()
  146. {
  147.   free(_filename);
  148.   free(_filename0);
  149.   free(_notifyCommand);
  150.   free(_notifyCommand0);
  151.   free(_advanceWarning);
  152.   free(_advanceWarning0);
  153. }
  154.  
  155. /**********************************************************************/
  156.  
  157. void
  158. Preferences::setFilename(const char *v)
  159. {
  160.   free(_filename);
  161.   _filename = pathexpandtilde(v);
  162. }
  163.  
  164. void
  165. Preferences::setAdvanceWarning(const char *v)
  166. {
  167.   char str[MAXSTR];
  168.  
  169.   free(_advanceWarning);
  170.   if (!strchr(v, ' ')) {
  171.     strcpy(str, v);
  172.     strcat(str, " min");
  173.     _advanceWarning = strdup(str);
  174.   } else {
  175.     _advanceWarning = strdup(v);
  176.   }
  177. }
  178.  
  179. void
  180. Preferences::setNotifyCommand(const char *v)
  181. {
  182.   free(_notifyCommand);
  183.   _notifyCommand = strdup(v);
  184. }
  185.  
  186. void
  187. Preferences::loadPreferences()
  188. {
  189.   char path[1024], str[1024];
  190.   FILE *fd;
  191.   int num;
  192.   Boolean resave;
  193.  
  194.   resave = False;
  195.   sprintf(path, "%sPref", _filename);
  196.   if (fd = fopen(path, "r")) {
  197.     while (fgets(str, sizeof(str), fd)) {
  198.       if (!strcmp(str, "filename\n")) {
  199.     if (readStr(fd, str)) {
  200.       setFilename(str);
  201.     } else {
  202.       fprintf(stderr, "VCal: garbled 'filename' preference\n");
  203.     }
  204.       } else if (!strcmp(str, "advanceWarning\n")) {
  205.     if (readStr(fd, str)) {
  206.       setAdvanceWarning(str);
  207.     } else {
  208.       fprintf(stderr, "VCal: garbled 'advanceWarning' preference\n");
  209.     }
  210.       } else if (!strcmp(str, "updateInterval\n")) {
  211.     if (readInt(fd, &num)) {
  212.       _updateInterval = num;
  213.     } else {
  214.       fprintf(stderr, "VCal: garbled 'updateInterval' preference\n");
  215.     }
  216.       } else if (!strcmp(str, "showLates\n")) {
  217.     if (readInt(fd, &num)) {
  218.       _showLates = num;
  219.     } else {
  220.       fprintf(stderr, "VCal: garbled 'showLates' preference\n");
  221.     }
  222.       } else if (!strcmp(str, "saveChangesAutomatically\n")) {
  223.     if (readInt(fd, &num)) {
  224.       _saveChangesAutomatically = num;
  225.     } else {
  226.       fprintf(stderr, "VCal: garbled 'saveChangesAutomatically' preference\n");
  227.     }
  228.       } else if (!strcmp(str, "annotateFile\n")) {
  229.     if (readInt(fd, &num)) {
  230.       _annotateFile = num;
  231.     } else {
  232.       fprintf(stderr, "VCal: garbled 'annotateFile' preference\n");
  233.     }
  234.       } else if (!strcmp(str, "updateIconName\n")) {
  235.     if (readInt(fd, &num)) {
  236.       _updateIconName = num;
  237.     } else {
  238.       fprintf(stderr, "VCal: garbled 'updateIconName' preference\n");
  239.     }
  240.       } else if (!strcmp(str, "updateIconDate\n")) {
  241.     if (readInt(fd, &num)) {
  242.       _updateIconDate = num;
  243.     } else {
  244.       fprintf(stderr, "VCal: garbled 'updateIconDate' preference\n");
  245.     }
  246.       } else if (!strcmp(str, "clock24\n")) {
  247.     if (readInt(fd, &num)) {
  248.       _clock24 = num;
  249.     } else {
  250.       fprintf(stderr, "VCal: garbled 'clock24' preference\n");
  251.     }
  252.       } else if (!strcmp(str, "displayAlarms\n")) {
  253.     if (readInt(fd, &num)) {
  254.       _alarms = num;
  255.     } else {
  256.       fprintf(stderr, "VCal: garbled 'displayAlarms' preference\n");
  257.     }
  258.       } else if (!strcmp(str, "notifyPopup\n")) {
  259.     if (readInt(fd, &num)) {
  260.       _notifyPopup = num;
  261.     } else {
  262.       fprintf(stderr, "VCal: garbled 'notifyPopup' preference\n");
  263.     }
  264.       } else if (!strcmp(str, "notifyBell\n")) {
  265.     if (readInt(fd, &num)) {
  266.       _notifyBell = num;
  267.     } else {
  268.       fprintf(stderr, "VCal: garbled 'notifyBell' preference\n");
  269.     }
  270.       } else if (!strcmp(str, "notifyMail\n")) {
  271.     if (readInt(fd, &num)) {
  272.       _notifyMail = num;
  273.     } else {
  274.       fprintf(stderr, "VCal: garbled 'notifyMail' preference\n");
  275.     } 
  276.       } else if (!strcmp(str, "notifyConfirmDelete\n")) {
  277.     if (readInt(fd, &num)) {
  278.       _notifyConfirmDelete = num;
  279.     } else {
  280.       fprintf(stderr, "VCal: garbled 'notifyConfirmDelete' preference\n");
  281.     }
  282.       } else if (!strcmp(str, "notifyCommand\n")) {
  283.     if (readStr(fd, str)) {
  284.       setNotifyCommand(str);
  285.     } else {
  286.       fprintf(stderr, "VCal: garbled 'notifyCommand' preference\n");
  287.     }
  288.       } else if (!strcmp(str, "annotateDay\n")) {
  289.     if (readInt(fd, &num)) {
  290.       _annotateDay = num;
  291.     } else {
  292.       fprintf(stderr, "VCal: garbled 'annotateDay' preference\n");
  293.     }
  294.       } else if (!strcmp(str, "snoozeMinutes\n")) {
  295.     if (readInt(fd, &num)) {
  296.       _snoozeMinutes = num;
  297.     } else {
  298.       fprintf(stderr, "VCal: garbled 'snoozeMinutes' preference\n");
  299.     }
  300.       } else if (!strcmp(str, "hideMenuBar\n")) {
  301.     if (readInt(fd, &num)) {
  302.       _hideMenuBar = num;
  303.     } else {
  304.       fprintf(stderr, "VCal: garbled 'hideMenuBar' preference\n");
  305.     }
  306.       } else if (!strcmp(str, "apptLength\n")) {
  307.     if (readInt(fd, &num)) {
  308.       _apptLength = num;
  309.     } else {
  310.       fprintf(stderr, "VCal: garbled 'apptLength' preference\n");
  311.     }
  312.       } else if (!strcmp(str, "gridLength\n")) {
  313.     if (readInt(fd, &num)) {
  314.       _gridLength = num;
  315.     } else {
  316.       fprintf(stderr, "VCal: garbled 'gridLength' preference\n");
  317.     }
  318.       } else if (!strcmp(str, "apptSubdiv\n")) {
  319.     if (readInt(fd, &num)) {
  320.       _apptSubdiv = num;
  321.     } else {
  322.       fprintf(stderr, "VCal: garbled 'apptSubdiv' preference\n");
  323.     }
  324.       } else if (!strcmp(str, "snapInterval\n")) {
  325.     if (readInt(fd, &num)) {
  326.       _snapInterval = num;
  327.     } else {
  328.       fprintf(stderr, "VCal: garbled 'snapInterval' preference\n");
  329.     }
  330.       } else if (!strcmp(str, "annotateMonth\n")) {
  331.     if (readInt(fd, &num)) {
  332.       _annotateMonth = num;
  333.     } else {
  334.       fprintf(stderr, "VCal: garbled 'annotateMonth' preference\n");
  335.     }
  336.       } else if (!strcmp(str, "restricted\n")) {
  337.     if (readInt(fd, &num)) {
  338.       _restricted = num;
  339.     } else {
  340.       fprintf(stderr, "VCal: garbled 'restricted' preference\n");
  341.     }
  342.       } else if (!strcmp(str, "restrictStart\n")) {
  343.     if (readInt(fd, &num)) {
  344.       _restrictStart = num;
  345.     } else {
  346.       fprintf(stderr, "VCal: garbled 'restrictStart' preference\n");
  347.     }
  348.       } else if (!strcmp(str, "restrictStop\n")) {
  349.     if (readInt(fd, &num)) {
  350.       _restrictStop = num;
  351.     } else {
  352.       fprintf(stderr, "VCal: garbled 'restrictStop' preference\n");
  353.     }
  354.       } else {
  355.     if (!strcasecmp(str, "displayExactAlarms\n")) {
  356.       resave = True;
  357.       theWarningDialog->post("The Also Post Exact Alarm preference is obsolete.\nIf you want to display exact alarms, add ', 0 min' to Alarm Advance Notice(s).");
  358.     } else {
  359.       fprintf(stderr, "VCal: unknown preference: %s", str);
  360.     }
  361.     readStr(fd, str);
  362.       }
  363.     }
  364.     fclose(fd);
  365.   }
  366.   if (resave) {
  367.     savePreferences();
  368.   }
  369. }
  370.  
  371. void
  372. Preferences::savePreferences()
  373. {
  374.   char path[1024];
  375.   FILE *fd;
  376.  
  377.   sprintf(path, "%sPref", _filename);
  378.   if (fd = fopen(path, "w")) {
  379.     if (strcmp(_filename, _filename0)) {
  380.       fprintf(fd, "filename\n%s\n", _filename);
  381.     }
  382.     if (strcmp(_advanceWarning, _advanceWarning0)) {
  383.       fprintf(fd, "advanceWarning\n%s\n", _advanceWarning);
  384.     }
  385.     if (_updateInterval != _updateInterval0) {
  386.       fprintf(fd, "updateInterval\n%d\n", _updateInterval);
  387.     }
  388.     if (_showLates != _showLates0) {
  389.       fprintf(fd, "showLates\n%d\n", _showLates);
  390.     }
  391.     if (_saveChangesAutomatically != _saveChangesAutomatically0) {
  392.       fprintf(fd, "saveChangesAutomatically\n%d\n", _saveChangesAutomatically);
  393.     }
  394.     if (_annotateFile != _annotateFile0) {
  395.       fprintf(fd, "annotateFile\n%d\n", _annotateFile);
  396.     }
  397.     if (_updateIconName != _updateIconName0) {
  398.       fprintf(fd, "updateIconName\n%d\n", _updateIconName);
  399.     }
  400.     if (_updateIconDate != _updateIconDate0) {
  401.       fprintf(fd, "updateIconDate\n%d\n", _updateIconDate);
  402.     }
  403.     if (_clock24 != _clock240) {
  404.       fprintf(fd, "clock24\n%d\n", _clock24);
  405.     }
  406.     if (_alarms != _alarms0) {
  407.       fprintf(fd, "displayAlarms\n%d\n", _alarms);
  408.     }
  409.     if (_notifyPopup != _notifyPopup0) {
  410.       fprintf(fd, "notifyPopup\n%d\n", _notifyPopup);
  411.     }
  412.     if (_notifyBell != _notifyBell0) {
  413.       fprintf(fd, "notifyBell\n%d\n", _notifyBell);
  414.     }
  415.     if (_notifyMail != _notifyMail0) {
  416.       fprintf(fd, "notifyMail\n%d\n", _notifyMail);
  417.     }
  418.     if (_notifyConfirmDelete != _notifyConfirmDelete0) {
  419.       fprintf(fd, "notifyConfirmDelete\n%d\n", _notifyConfirmDelete);
  420.     }
  421.     if (strcmp(_notifyCommand, _notifyCommand0)) {
  422.       fprintf(fd, "notifyCommand\n%s\n", _notifyCommand);
  423.     }
  424.     if (_annotateDay != _annotateDay0) {
  425.       fprintf(fd, "annotateDay\n%d\n", _annotateDay);
  426.     }
  427.     if (_snoozeMinutes != _snoozeMinutes0) {
  428.       fprintf(fd, "snoozeMinutes\n%d\n", _snoozeMinutes);
  429.     }
  430.     if (_hideMenuBar != _hideMenuBar0) {
  431.       fprintf(fd, "hideMenuBar\n%d\n", _hideMenuBar);
  432.     }
  433.     if (_apptLength != _apptLength0) {
  434.       fprintf(fd, "apptLength\n%d\n", _apptLength);
  435.     }
  436.     if (_gridLength != _gridLength0) {
  437.       fprintf(fd, "gridLength\n%d\n", _gridLength);
  438.     }
  439.     if (_apptSubdiv != _apptSubdiv0) {
  440.       fprintf(fd, "apptSubdiv\n%d\n", _apptSubdiv);
  441.     }
  442.     if (_snapInterval != _snapInterval0) {
  443.       fprintf(fd, "snapInterval\n%d\n", _snapInterval);
  444.     }
  445.     if (_annotateMonth != _annotateMonth0) {
  446.       fprintf(fd, "annotateMonth\n%d\n", _annotateMonth);
  447.     }
  448.     if (_restricted != _restricted0) {
  449.       fprintf(fd, "restricted\n%d\n", _restricted);
  450.     }
  451.     if (_restrictStart != _restrictStart0) {
  452.       fprintf(fd, "restrictStart\n%d\n", _restrictStart);
  453.     }
  454.     if (_restrictStop != _restrictStop0) {
  455.       fprintf(fd, "restrictStop\n%d\n", _restrictStop);
  456.     }
  457.     fclose(fd);
  458.   }
  459. }
  460.  
  461. void
  462. Preferences::useFactory()
  463. {
  464.   setFilename(_filename0);
  465.   setAdvanceWarning(_advanceWarning0);
  466.   _updateInterval = _updateInterval0;
  467.   _showLates = _showLates0;
  468.   _saveChangesAutomatically = _saveChangesAutomatically0;
  469.   _annotateFile = _annotateFile0;
  470.   _updateIconName = _updateIconName0;
  471.   _updateIconDate = _updateIconDate0;
  472.   _clock24 = _clock240;
  473.   _alarms = _alarms0;
  474.   _notifyPopup = _notifyPopup0;
  475.   _notifyBell = _notifyBell0;
  476.   _notifyMail = _notifyMail0;
  477.   _notifyConfirmDelete = _notifyConfirmDelete0;
  478.   setNotifyCommand(_notifyCommand0);
  479.   _annotateDay = _annotateDay0;
  480.   _snoozeMinutes = _snoozeMinutes0;
  481. // Don't revert menubar setting
  482. //  _hideMenuBar = _hideMenuBar0;
  483.   _apptLength = _apptLength0;
  484.   _gridLength = _gridLength0;
  485.   _apptSubdiv = _apptSubdiv0;
  486.   _snapInterval = _snapInterval0;
  487.   _annotateMonth = _annotateMonth0;
  488.   _restricted = _restricted0;
  489.   _restrictStart = _restrictStart0;
  490.   _restrictStop = _restrictStop0;
  491. }
  492.